package com.cqfae.deploycentral.app.web.controller;

import com.cqfae.deploycentral.app.services.AppInfoService;
import com.cqfae.deploycentral.app.services.CommonControllerService;
import com.cqfae.deploycentral.app.utils.GsonUtils;
import com.cqfae.deploycentral.app.utils.NullUtil;
import com.cqfae.deploycentral.app.web.request.AppInfoRequest;
import com.cqfae.deploycentral.app.web.response.WebResponse;
import com.cqfae.deploycentral.app.web.vo.AppInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TAppInfo相关操作
 *
 * @author JIANGLU009
 * @since 1.0.0-snapshot
 */
@RestController
@RequestMapping("/deploy-central/app/info")
public class AppInfoController implements CommonControllerService<Object> {

    private static Logger logger = LoggerFactory.getLogger(AppInfoController.class);

    @Autowired
    private AppInfoService appInfoService;

    /**
     * 新增
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/add")
    public WebResponse add(@RequestBody AppInfoVo vo) {
        String voJson = GsonUtils.getInstance().toJson(vo);
        logger.info("==== add EnvAppVo :==== {}", voJson);
        // 参数空验证
        parameterCheck(vo);
        return getWebResponse(appInfoService.add(vo));
    }

    /**
     * 编辑
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    public WebResponse edit(@RequestBody AppInfoVo vo) {
        String voJson = GsonUtils.getInstance().toJson(vo);
        logger.info("==== edit EnvAppVo :==== {}", voJson);
        // 参数空验证
        parameterCheck(vo);
        return getWebResponse(appInfoService.edit(vo));
    }

    /**
     * 删除
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    public WebResponse delete(@RequestBody AppInfoVo vo) {
        String voJson = GsonUtils.getInstance().toJson(vo);
        logger.info("==== delete EnvAppVo :==== {}", voJson);
        return getWebResponse(appInfoService.delete(vo));
    }

    /**
     * 链表
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    public WebResponse list(AppInfoRequest request) {
        String requestJson = GsonUtils.getInstance().toJson(request);
        logger.info("==== list EnvInfoRequest :==== {}", requestJson);
        return getWebResponse(appInfoService.list(request));
    }

    /**
     * 翻页查询
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list-by-page")
    public WebResponse listByPage(AppInfoRequest request) {
        String requestJson = GsonUtils.getInstance().toJson(request);
        logger.info("==== listByPage EnvInfoRequest :==== {}", requestJson);
        return getWebResponse(appInfoService.listByPage(request));
    }

    /**
     * 根据ID查询一个
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/get-one")
    public WebResponse getOne(AppInfoRequest request) {
        String requestJson = GsonUtils.getInstance().toJson(request);
        logger.info("==== info EnvAppRequest :==== {}", requestJson);
        return getWebResponse(appInfoService.getOne(request));
    }

    /**
     * 获得所有有效的appName
     *
     * @return
     */
    @RequestMapping(value = "/get-app-name-list")
    public WebResponse getAppNameList(AppInfoRequest request) {
        logger.info("==== getAppNameList EnvAppRequest :==== ");
        return getWebResponse(appInfoService.getAppNameList(request));
    }

    /**
     * 设置返回参数
     *
     * @param obj
     * @return
     */
    @Override
    public WebResponse getWebResponse(Object obj) {
        WebResponse webResponse = new WebResponse();
        if (NullUtil.isNotEmpty(obj)) {
            webResponse.setData(obj);
        }
        return webResponse;
    }

    /**
     * 传入参数验证
     *
     * @param vo
     */
    private void parameterCheck(AppInfoVo vo) {
        Assert.isTrue(!StringUtils.isEmpty(vo.getAppName()), "项目名称为空");
        Assert.isTrue(!StringUtils.isEmpty(vo.getGitPath()), "git地址为空");
        Assert.isTrue(vo.getBuildType() != null, "构建类型为空");
        Assert.isTrue(!StringUtils.isEmpty(vo.getLeaderName()), "负责人为空");
        Assert.isTrue(!StringUtils.isEmpty(vo.getPackageName()), "包名为空");
        Assert.isTrue(!StringUtils.isEmpty(vo.getBuildUrl()), "构建接口为空");
        Assert.isTrue(vo.getPort() != null, "端口号为空");
        Assert.isTrue(vo.getPackageType() != null, "包类型为空");
        Assert.isTrue(!StringUtils.isEmpty(vo.getCheckUrl()), "验证URL为空");
    }

}
